﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnitTest.TaskSafetyDemo
{
    public class TaskSafety2
    {
        static int value = 100;
        public static void Go()
        {
            for (int i = 0; i < 5000; i++)
            {
                DoTest();
            }
        }

        public static void DoTest()
        {
            List<Task> tasks = new List<Task>();
            var task1 = Task.Run(() =>
            {
                AddValue();
                DelValue();
                Console.WriteLine(value);
            });
            var task2 = Task.Run(() =>
            {
                AddValue();
                DelValue();
                Console.WriteLine(value);
            });
            tasks.Add(task1);
            tasks.Add(task2);
            Task.WaitAll(tasks.ToArray());
        }

        public static void AddValue() 
        {
                value++;
        }

        public static void DelValue()
        {
                value--;
        }
    }
}
